package com.server.commons.sms.sms;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;

import com.server.model.all.system.bo.AddSmsLogOperationBo;
import com.server.model.all.system.bo.JHSendSmsResponseBo;
import com.server.liad.api.system.remote.RemoteSystemService;
import com.server.model.all.constant.sms.JHSendSmsTplConstant;
import com.server.model.all.enums.sms.JHSendSmsStatusEnum;
import com.server.model.all.enums.sms.SendSmsStatusEnum;
import com.server.model.all.enums.sms.SmsPlatformEnum;
import com.server.model.all.enums.sms.SmsTypeEnum;
import com.server.common.code.error.BusinessException;
import com.server.common.code.utils.GsonUtils;
import com.server.common.code.utils.HttpUtils;
import com.server.common.code.vo.LoginVo;
import com.server.common.redis.service.RedisSmsService;
import com.server.common.security.code.SecurityUtils;
import com.server.commons.rabbitmq.MyRabbitTemplate;
import com.server.commons.rabbitmq.constant.LogRabbitMQConstant;
import com.server.commons.rabbitmq.model.MyCorrelationData;
import com.server.commons.sms.base.BaseSmsInterface;
import com.server.commons.sms.bo.SendSmsBo;
import com.server.commons.sms.config.JuHeSmsConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 聚合短信
 * @author LXC
 * @date 2022/6/15 21:54
 */
@Component
@Slf4j
public class JuHeSmsCommon extends BaseSmsInterface {

    @Resource
    private JuHeSmsConfig juHeSmsConfig;

    @Resource
    private RemoteSystemService remoteSystemService;

    @Resource
    private MyRabbitTemplate rabbitTemplate;

    @Resource
    private RedisSmsService redisSmsService;

    @Override
    public void sendSms(SendSmsBo sendSmsBo) {
        super.sendSms(sendSmsBo);

        LoginVo loginVo = SecurityUtils.getLoginVo();

        Map<String,Object> map = new HashMap<>();
        map.put("tpl_id",sendSmsBo.getTplId());
        map.put("mobile",sendSmsBo.getMobile());
        map.put("tpl_value","#code#="+sendSmsBo.getCode());
        map.put("key", juHeSmsConfig.getSmsKey());
        HttpRequest httpRequest = HttpUtil.createGet(juHeSmsConfig.getSmsUrl()+"?"+ HttpUtils.mapToGetParam(map));
        String body = httpRequest.execute().body();

        JHSendSmsResponseBo jhSendSmsResponseBo = GsonUtils.getGson().fromJson(body, JHSendSmsResponseBo.class);

        if(!JHSendSmsStatusEnum.YES.getCode().equals(jhSendSmsResponseBo.getErrorCode())){
            log.error("聚合短信发送失败:"+ jhSendSmsResponseBo.getReason()+"--"+ jhSendSmsResponseBo.getErrorCode());
            throw new BusinessException("短信发送失败!");
        }

        log.error("聚合短信发送成功:"+sendSmsBo.getMobile()+"--"+sendSmsBo.getTplId()+"--"+sendSmsBo.getCode());

        //短信记录
        AddSmsLogOperationBo addSmsLogOperationBo = AddSmsLogOperationBo.builder()
                .code(sendSmsBo.getCode())
                .mobile(sendSmsBo.getMobile())
                .tplId(sendSmsBo.getTplId())
                .smsType(sendSmsBo.getSmsTypeEnum().getCode())
                .platform(SmsPlatformEnum.JUHE.getCode())
                .status(SendSmsStatusEnum.SEND.getCode())
                .userId(ObjectUtils.isEmpty(loginVo)?null: loginVo.getUserId())
                .build();

        MyCorrelationData myCorrelationData = new MyCorrelationData();
        myCorrelationData.setExchangeName(LogRabbitMQConstant.LOG_EXCHANGE);
        myCorrelationData.setRoutingKey(LogRabbitMQConstant.SMS_OPERATION_LOG_ROUTING_KEY);
        myCorrelationData.setBody(GsonUtils.getGson().toJson(addSmsLogOperationBo));
        rabbitTemplate.convertAndSend(myCorrelationData);

    }

    /**
     * 发注册短信
     *
     * @author LXC
     * @date 2022/6/16 22:31
     */
    @Override
    public void sendRegisterSms(String mobile, String code) {
        SendSmsBo sendSmsBo = SendSmsBo.builder()
                .tplId(JHSendSmsTplConstant.REGISTER_SMS_TPL)
                .mobile(mobile)
                .code(code)
                .smsTypeEnum(SmsTypeEnum.REGISTER).build();

        redisSmsService.addSmsCode(SmsTypeEnum.REGISTER.getSmsTypeRedisKey(), mobile,code);

        sendSms(sendSmsBo);
    }

    /**
     * 发登录短信
     *
     * @author LXC
     * @date 2022/6/16 22:31
     */
    @Override
    public void sendLoginSms(String mobile, String code) {
        SendSmsBo sendSmsBo = SendSmsBo.builder()
                .tplId(JHSendSmsTplConstant.LOGIN_SMS_TPL)
                .mobile(mobile)
                .code(code)
                .smsTypeEnum(SmsTypeEnum.LOGIN).build();

        redisSmsService.addSmsCode(SmsTypeEnum.LOGIN.getSmsTypeRedisKey(), mobile,code);

        sendSms(sendSmsBo);
    }

    /**
     * 发忘记密码短信
     *
     * @author LXC
     * @date 2022/6/16 22:31
     */
    @Override
    public void sendSmsForgetPassword(String mobile, String code) {
        SendSmsBo sendSmsBo = SendSmsBo.builder()
                .tplId(JHSendSmsTplConstant.FORGET_PASSWORD_SMS_TPL)
                .mobile(mobile)
                .code(code)
                .smsTypeEnum(SmsTypeEnum.FORGET_PASSWORD).build();

        redisSmsService.addSmsCode(SmsTypeEnum.FORGET_PASSWORD.getSmsTypeRedisKey(), mobile,code);

        sendSms(sendSmsBo);
    }

}
